package com.powerunion.datacollection.web.struts;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.powerunion.datacollection.service.ItemCategoryManager;
import com.powerunion.datacollection.transaction.Transaction;
import com.powerunion.datacollection.transaction.TransactionHelper;
import com.powerunion.util.StringUtils;
import com.powerunion.util.log.LogHelper;

public class ItemCategoryAction extends BaseAction {

	private static final Logger logger = LogHelper.getInstance().getLogger(ItemCategoryAction.class);
	private ItemCategoryManager icManager = null;
	
	public ItemCategoryAction() {
		super();
		this.icManager = new ItemCategoryManager();
	}
	
	public void finalize() throws Throwable {
		this.icManager = null;
		super.finalize();
	}
	
	public ActionForward save (ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response)
	        throws Exception {
		
		String saveStr = request.getParameter("saveStr");
		int categoryType = Integer.parseInt(request.getParameter("categoryType"));

		Transaction transaction = null;
		try {
			transaction = TransactionHelper.getTransaction();
			transaction.begin();
			logger.debug("类别信息定义:"+saveStr);
			
			String id = this.icManager.save(saveStr,categoryType);
			
	        if(id != null && !id.equals("")) {
	        	response.getWriter().write("{success:true,data:'"+id+"'}");
	        }else{
	        	response.getWriter().write("{failure:true}");
	        }
			
			transaction.commit();
		} catch (Exception e) {
			logger.error("", e);
			try {
				transaction.rollback();
			} catch (Exception e1) {
				logger.error("", e1);
				throw e1;
			} finally {
				transaction = null;
				response.getWriter().write("{failure:true}");
			}
		}

		return null;
	}
	
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		String[] ids = StringUtils.split(request.getParameter("codes"), ",");
		
		Transaction transaction = null;

		response.setContentType("text/json;charset=utf-8");
		try {
			transaction = TransactionHelper.getTransaction();
			transaction.begin();

			boolean flag = this.icManager.delete(ids);

			if (flag == true) {
				response.getWriter().print("{success:true}");
			} else {
				response.getWriter().write("{failure:true}");
			}
			transaction.commit();
		} catch (Exception e) {
			try {
				transaction.rollback();
			} catch (Exception e1) {
				logger.error("", e);
				throw e;
			} finally {
				transaction = null;
				response.getWriter().write("{failure:true}");
			}
		}
		return null;
	}
		
	public ActionForward getTree(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		//System.out.println("host:"+request.getRemoteHost()+"||address:"+request.getRemoteAddr());
		String paraSeq =  request.getParameter("node");	
		int categoryType = Integer.parseInt(request.getParameter("categoryType"));	
		String userCode = request.getRemoteUser();
		logger.debug("getTree paraSeq:"+paraSeq+"||categoryType:"+categoryType+"||userCode:"+userCode);
		//int permissionType = Integer.parseInt(request.getParameter("permissionType"));	
		
		JSONArray jsonArray = this.icManager.getTree(paraSeq, categoryType, userCode, 1);
logger.debug("tree :"+jsonArray.toString());
		response.setContentType("text/json;charset=utf-8");
		try {
			response.getWriter().print(jsonArray.toString());
		} catch (IOException e) {
			logger.error("输出json失败",e);
			return null;
		}
		return null;
	}
	
}
